//
//  TimeUpTitleView.m
//  TerritoryShopkeeper
//
//  Created by xiaowu on 2017/12/7.
//  Copyright © 2017年 ZDQK. All rights reserved.
//

#import "TimeUpTitleView.h"
#import "TimeUpTitlesItemCell.h"

@interface TimeUpTitleView () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation TimeUpTitleView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self configure];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configure];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.collectionView.frame = self.bounds;
}

#pragma mark - Method
- (void)configure {
    self.selectIndex = 0;
    [self addSubview:self.collectionView];
}

- (void)setDataSource:(NSMutableArray *)dataSource withBlock:(void (^)(NSInteger index))block {
    self.block = block;
    _dataSource = dataSource;
    for (NSInteger i = 0; i < dataSource.count; i++) {
        TimeUpTimeItem *model = dataSource[i];
        if (model.state == 1) {
            model.isSelect = YES;
            self.selectIndex = i;
            if (self.block) {
                self.block(self.selectIndex);
            }
            break;
        } else {
            model.isSelect = NO;
        }
    }
    if (_dataSource.count > 0) {
        TimeUpTimeItem *model = _dataSource.firstObject;
        if (model.state == 0 && model.isSelect == NO) {
            model.isSelect = YES;
        }
    }
    
    [self.collectionView reloadData];
//    _dataSource = [NSMutableArray arrayWithArray:dataSource];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataSource.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    TimeUpTitlesItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"itemCell" forIndexPath:indexPath];
    
    TimeUpTimeItem *item = [self.dataSource objectAtIndex:indexPath.row];
    
    cell.timeLabel.text = [DateSomeThing getDateStr:item.sktime inDateFormat:@"HH:mm:ss" outDateFormat:@"HH:mm"];
    cell.titleLabel.text = item.title;
    
    if (item.isSelect == 1) {
        cell.currentType = TimerUpTitlesItemCellTypeForSelected;
    } else {
        cell.currentType = TimerUpTitlesItemCellTypeForNone;
    }
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(self.width * 0.2, self.height);
}
#pragma mark - UITableViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    for (TimeUpTimeItem *item in self.dataSource) {
        item.isSelect = 0;
    }
    TimeUpTimeItem *item = [self.dataSource objectAtIndex:indexPath.row];
    item.isSelect = 1;
    
    [collectionView reloadData];
    
    if (self.block) {
        self.selectIndex = indexPath.row;
        self.block(indexPath.row);
    }
}

#pragma mark - Getters
- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        flowLayout.itemSize = self.bounds.size;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal; // 横向
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collectionView.pagingEnabled = YES;
        _collectionView.alwaysBounceHorizontal = YES;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.bounces = NO;
        
        [_collectionView registerNib:[UINib nibWithNibName:@"TimeUpTitlesItemCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"itemCell"];
    }
    return _collectionView;
}

@end
